Array.prototype.derangedArray = function () {
    for (var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
    return this;
};
/**
 * @description 全排列
 * @param {number[]} nums
 * @return {number[][]}
 */
var permute = function (nums) {
    if (nums.length <= 1) {
        return [nums]
    }
    let result = [[...nums]];
    let flag = true;
    var length = fac(nums.length);
    var str = result.map(v => v.toString());
    while (flag) {
        let temp = nums.derangedArray();
        if (!str.includes(temp.toString())) {
            result.push([...temp]);
            str = result.map(v => v.toString());
        }
        if (result.length === length) {
            flag = false;
        }
    }
    return result;
};
function fac(n) {
    return n > 1 ? n * fac(n - 1) : 1
}
console.log(permute([1, 2, 3]))
console.log(permute([0, 1]))
console.log(permute([1]))